/*
  ******************************************************************************
  * @file    RGB_demo.c
  * @author  FreqChip Firmware Team
  * @version V1.0.0
  * @date    2023
  * @brief   RGB interface module Demo.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 FreqChip.
  * All rights reserved.
  ******************************************************************************
*/
#include "app_lvgl.h"
#include "driver_st2568_rgb.h"

#if (BOARD_SEL == BOARD_EVB_FR3092E_RGB)

#ifdef DISPLAY_TYPE_ST2568_RGB

static GPIO_InitTypeDef GPIO_Handle;
PARALLEL_HandTypeDef hparallel;
SPI_HandleTypeDef    spi_handle;
DMA_HandleTypeDef dma_handle;
DMA_HandleTypeDef dma1_handle;
struct_RGB_TypeDef_t rgb_handle;
unsigned char *coply = NULL;

#if 1
#define RGB_VBPD    16// Vertical back porch
#define RGB_VFPD    16 // Vertical front porch
#define RGB_VSPW    4 // Vertical sync pulse width
//----------------------
#define RGB_HBPD    8 // Horizontal back porch
#define RGB_HFPD    8 // Horizontal front porch
#define RGB_HSPW    4 // Horizontal sync pulse width
#else
#define RGB_VBPD    8// Vertical back porch
#define RGB_VFPD    8 // Vertical front porch
#define RGB_VSPW    4 // Vertical sync pulse width
//----------------------
#define RGB_HBPD    4 // Horizontal back porch
#define RGB_HFPD    4 // Horizontal front porch
#define RGB_HSPW    2 // Horizontal sync pulse width

#endif

void timer0_irq(void);
//uint8_t test_buff[800*2];
uint16_t test_buff[800];
#ifdef RGB56_LCD_INIT_CONFIG
static void rgb_reg_writer(uint16_t *fp_data, uint32_t size)
{
    __RGB_SPI_CS_SET(); 
    /* fp_Data[0] stores command, while others store data
       Bit8 set to 1 as data, otherwise it is a command
    */
    for(int i = 1; i < size; i++)
    {
      fp_data[i] = fp_data[i] + 0x100;
    }
    
    __SPI_WRITE_DATA(fp_data,size); 
    
    __RGB_SPI_CS_RELEASE();
}
#endif
__RAM_CODE static void rgb_idle_clock(uint32_t count)
{
    /* 8080 hits idle Clocks */
    __8080_DATA_WR_LEN(count);
    for(int i = 0; i < count/2; i++)
        __8080_WRITE_BLANK();
}


__RAM_CODE static void Vertical_back_porch_set(void)
{
    while(!(__8080_TXFIFO_EMPTY()));
    __RGB_LCD_DENABLE_SET();
    __RGB_LCD_HSYNC__RELEASE();
     rgb_idle_clock(RGB_HSPW);
    // while(!(__8080_TXFIFO_EMPTY()));

    __RGB_LCD_HSYNC_SET();
    rgb_idle_clock(RGB_HBPD - RGB_HSPW);
    __NOP();
    __NOP();
    __NOP();
    __NOP();

    __NOP();
    __NOP();
    __NOP();
    __NOP();

    __NOP();
    __NOP();
    __NOP();
    __NOP();

    __NOP();
    __NOP();
    __NOP();
    __NOP();

    __NOP();
    __NOP();
    __NOP();
    __NOP();

    __NOP();
    __NOP();
    __NOP();
    __NOP();

    // rgb_idle_clock(RGB_HBPD - RGB_HSPW);   
    //rgb_idle_clock(RGB_HBPD - RGB_HSPW); 

    //while(!(__8080_TXFIFO_EMPTY()));
}

__RAM_CODE static void Vertical_front_porch_set(void)
{
    __RGB_LCD_HSYNC_SET();
    __RGB_LCD_DENABLE_SET();

     //rgb_idle_clock(RGB_HFPD);//HFP 

    // while(!(__8080_TXFIFO_EMPTY()));
}

__RAM_CODE static void Line_porch_set(uint32_t count)
{
    for(int i = 0; i < count; i++)
    {
        __RGB_LCD_HSYNC__RELEASE();
     //   __RGB_LCD_DENABLE_RELEASE();
		__RGB_LCD_DENABLE_SET();
        rgb_idle_clock(4);

        __RGB_LCD_HSYNC_SET();
        rgb_idle_clock(RGB_ROW);

    }
}

void rgb_init(void)
{
    uint16_t WBuffer[20];
    __RGB_LCD_RESET_SET();   
    system_delay_us(1000 * 5);
    __RGB_LCD_RESET_RELEASE();
    system_delay_us(50 * 1000); 
    __RGB_LCD_RESET_SET();   
    system_delay_us(1000 * 100);

	#ifdef RGB56_LCD_INIT_CONFIG
	#if 1
    WBuffer[0] = 0xB7;
    WBuffer[1] = 0x50;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);//  LP mode(DCS mode & HS clk disable)

    WBuffer[0] = 0xB8;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3); //VC(Virtual ChannelID) Control Register

    WBuffer[0] = 0xb9;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 20);//1=PLL disable
	
	#if 0
    WBuffer[0] = 0xBA;//TX_CLK/MS should be between 5Mhz to100Mhz
    WBuffer[1] = 0x14;
    WBuffer[2] = 0x42;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 20); 
	
	WBuffer[0] = 0xBB; 
    WBuffer[1] = 0x03;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 20); 
	#else
	WBuffer[0] = 0xBA;
    WBuffer[1] = 0x33;
    rgb_reg_writer(WBuffer, 2);
	system_delay_us(1000 * 20); 
	
	WBuffer[0] = 0xBB; 
    WBuffer[1] = 0x03;
    rgb_reg_writer(WBuffer, 2);
	system_delay_us(1000 * 20); 
	
	#endif
	
	
	WBuffer[0] = 0xB9; 
    WBuffer[1] = 0x01;//1=PLL  
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 200); 
	
	WBuffer[0] = 0xDE; 
    WBuffer[1] = 0x03;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 20); 
	
	WBuffer[0] = 0xC9; 
    WBuffer[1] = 0x02;
    WBuffer[2] = 0x23;
    rgb_reg_writer(WBuffer, 3);
	system_delay_us(1000 * 200); 
		
	WBuffer[0] = 0xB7; 
    WBuffer[1] = 0x10;
    WBuffer[2] = 0x02;
    rgb_reg_writer(WBuffer, 3);//10=TX_CLK 30=PCLK
	
	WBuffer[0] = 0xBD; 
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);
	
//******************************************************************************/
//* Panel Name : HSD4.0IPS(HSD040BPN1-A00)                                     */
//* Resulation : 480x480                                                       */
//* Inversion  : 2dot                                                          */
//* Porch      : vbp=23 , vfp=28                                               */
//* Line Time  : 32us                                                          */
//* Frame Rate : 65hz                                                          */
//******************************************************************************/
	WBuffer[0] = 0x11; 
	rgb_reg_writer(WBuffer, 1);
	system_delay_us(1000 * 120); 
	
    WBuffer[0] = 0xff;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x10;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0xc0;
    WBuffer[1] = 0x3B;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xc1;
    WBuffer[1] = 0x0d;
    WBuffer[2] = 0x02;
    rgb_reg_writer(WBuffer, 3);
	
	WBuffer[0] = 0xc2; //65hz
    WBuffer[1] = 0x21;
    WBuffer[2] = 0x08;
    rgb_reg_writer(WBuffer, 3);  //PCLK=512+(0X08*16)=640

//    WBuffer[0] = 0xC2;
//    WBuffer[1] = 0x21;
//    WBuffer[2] = 0x0A;
//    rgb_reg_writer(WBuffer, 3);
	
	WBuffer[0] = 0xB0;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x11;
    WBuffer[3] = 0x18;
    WBuffer[4] = 0x0E;
    WBuffer[5] = 0x11;
    WBuffer[6] = 0x06;
    WBuffer[7] = 0x07;
    WBuffer[8] = 0x08;
    WBuffer[9] = 0x07;
    WBuffer[10] = 0x22;
    WBuffer[11] = 0x04;
    WBuffer[12] = 0x12;
    WBuffer[13] = 0x0F;
    WBuffer[14] = 0xaa;
    WBuffer[15] = 0x31;
    WBuffer[16] = 0x18;
    rgb_reg_writer(WBuffer, 17);
	
    WBuffer[0] = 0xB1;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x11;
    WBuffer[3] = 0x19;
    WBuffer[4] = 0x0E;
    WBuffer[5] = 0x12;
    WBuffer[6] = 0x07;
    WBuffer[7] = 0x08;
    WBuffer[8] = 0x08;
    WBuffer[9] = 0x08;
    WBuffer[10] = 0x22;
    WBuffer[11] = 0x04;
    WBuffer[12] = 0x11;
    WBuffer[13] = 0x11;
    WBuffer[14] = 0xA9;
    WBuffer[15] = 0x32;
    WBuffer[16] = 0x18;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xFF;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x11;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0xB0;
    WBuffer[1] = 0x60;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB1;
    WBuffer[1] = 0x30;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB2;
    WBuffer[1] = 0x87;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB3;
    WBuffer[1] = 0x80;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB5;
    WBuffer[1] = 0x49;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB7;
    WBuffer[1] = 0x85;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB8;
    WBuffer[1] = 0x21;
    rgb_reg_writer(WBuffer, 2);

//    WBuffer[0] = 0xC0;
//    WBuffer[1] = 0x07;
//    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xC1;
    WBuffer[1] = 0x78;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xC2;
    WBuffer[1] = 0x78;
    rgb_reg_writer(WBuffer, 2);
	system_delay_us(1000*20);

    WBuffer[0] = 0xE0;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x1B;
    WBuffer[3] = 0x02;
    rgb_reg_writer(WBuffer, 4);

    WBuffer[0] = 0xE1;
    WBuffer[1] = 0x08;
    WBuffer[2] = 0xA0;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x07;
    WBuffer[6] = 0xA0;
    WBuffer[7] = 0x00;
    WBuffer[8] = 0x00;
    WBuffer[9] = 0x00;
    WBuffer[10] = 0x44;
    WBuffer[11] = 0x44;
    rgb_reg_writer(WBuffer, 12);

    WBuffer[0] = 0xE2;
    WBuffer[1] = 0x11;
    WBuffer[2] = 0x11;
    WBuffer[3] = 0x44;
    WBuffer[4] = 0x44;
    WBuffer[5] = 0xED;
    WBuffer[6] = 0xA0;
    WBuffer[7] = 0x00;
    WBuffer[8] = 0x00;
    WBuffer[9] = 0xEC;
    WBuffer[10] = 0xA0;
    WBuffer[11] = 0x00;
    WBuffer[12] = 0x00;
    rgb_reg_writer(WBuffer, 13);

    WBuffer[0] = 0xE3;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x11;
    WBuffer[4] = 0x11;
    rgb_reg_writer(WBuffer, 5);

    WBuffer[0] = 0xE4;
    WBuffer[1] = 0x44;
    WBuffer[2] = 0x44;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xE5;
    WBuffer[1] = 0x0a;
    WBuffer[2] = 0xE9;
    WBuffer[3] = 0xD8;
    WBuffer[4] = 0xA0;
    WBuffer[5] = 0x0C;
    WBuffer[6] = 0xEB;
    WBuffer[7] = 0xD8;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0x0E;
    WBuffer[10] = 0xED;
    WBuffer[11] = 0xD8;
    WBuffer[12] = 0xA0;
    WBuffer[13] = 0x10;
    WBuffer[14] = 0xEF;
    WBuffer[15] = 0xD8;
    WBuffer[16] = 0xA0;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xE6;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x11;
    WBuffer[4] = 0x11;
    rgb_reg_writer(WBuffer, 5);

    WBuffer[0] = 0xE7;
    WBuffer[1] = 0x44;
    WBuffer[2] = 0x44;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xE8;
    WBuffer[1] = 0x09;
    WBuffer[2] = 0xE8;
    WBuffer[3] = 0xD8;
    WBuffer[4] = 0xA0;
    WBuffer[5] = 0x0B;
    WBuffer[6] = 0xEA;
    WBuffer[7] = 0xD8;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0x0D;
    WBuffer[10] = 0xEC;
    WBuffer[11] = 0xD8;
    WBuffer[12] = 0xA0;
    WBuffer[13] = 0x0F;
    WBuffer[14] = 0xEE;
    WBuffer[15] = 0xD8;
    WBuffer[16] = 0xA0;
    rgb_reg_writer(WBuffer, 17);

//    WBuffer[0] = 0xE9;
//    WBuffer[1] = 0x36;
//    WBuffer[2] = 0x00;
//    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xEB;
    WBuffer[1] = 0x02;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0xE4;
    WBuffer[4] = 0xE4;
    WBuffer[5] = 0x88;
    WBuffer[6] = 0x00;
    WBuffer[7] = 0x40;
    rgb_reg_writer(WBuffer, 8);

    WBuffer[0] = 0xEC;
    WBuffer[1] = 0x3C;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xED;
    WBuffer[1] = 0xAB;
    WBuffer[2] = 0x89;
    WBuffer[3] = 0x76;
    WBuffer[4] = 0x54;
    WBuffer[5] = 0x02;
    WBuffer[6] = 0xff;
    WBuffer[7] = 0xff;
    WBuffer[8] = 0xff;
    WBuffer[9] = 0xff;
    WBuffer[10] = 0xff;
    WBuffer[11] = 0xff;
    WBuffer[12] = 0x20;
    WBuffer[13] = 0x45;
    WBuffer[14] = 0x67;
    WBuffer[15] = 0x98;
    WBuffer[16] = 0xBA;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xFF;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x00;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0xC3;
    WBuffer[1] = 0x00;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0x36;
    WBuffer[1] = 0x00;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0x3A;
    WBuffer[1] = 0x70;
    rgb_reg_writer(WBuffer, 2);

//    WBuffer[0] = 0x21;  
//    rgb_reg_writer(WBuffer,1);
    WBuffer[0] = 0x29;  
    rgb_reg_writer(WBuffer,1);

	#else
	
	 WBuffer[0] = 0xFF;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x10;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0xC0;
    WBuffer[1] = 0x3B;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xC1;
    WBuffer[1] = 0x10;
    WBuffer[2] = 0x0C;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xC2;
    WBuffer[1] = 0x21;
    WBuffer[2] = 0x0A;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xB0;
    WBuffer[1] = 0x40;
    WBuffer[2] = 0x09;
    WBuffer[3] = 0x4F;
    WBuffer[4] = 0x0B;
    WBuffer[5] = 0x10;
    WBuffer[6] = 0x07;
    WBuffer[7] = 0x00;
    WBuffer[8] = 0x08;
    WBuffer[9] = 0x06;
    WBuffer[10] = 0x20;
    WBuffer[11] = 0x02;
    WBuffer[12] = 0x12;
    WBuffer[13] = 0x0F;
    WBuffer[14] = 0x67;
    WBuffer[15] = 0x2E;
    WBuffer[16] = 0xDF;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xB1;
    WBuffer[1] = 0x4F;
    WBuffer[2] = 0x18;
    WBuffer[3] = 0x60;
    WBuffer[4] = 0x0E;
    WBuffer[5] = 0x10;
    WBuffer[6] = 0x04;
    WBuffer[7] = 0x0C;
    WBuffer[8] = 0x08;
    WBuffer[9] = 0x09;
    WBuffer[10] = 0x26;
    WBuffer[11] = 0x07;
    WBuffer[12] = 0x13;
    WBuffer[13] = 0x11;
    WBuffer[14] = 0x71;
    WBuffer[15] = 0x39;
    WBuffer[16] = 0xDF;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xFF;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x11;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0xB0;
    WBuffer[1] = 0x4D;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB1;
    WBuffer[1] = 0x41;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB2;
    WBuffer[1] = 0x87;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB3;
    WBuffer[1] = 0x80;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB5;
    WBuffer[1] = 0x49;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB7;
    WBuffer[1] = 0x87;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xB8;
    WBuffer[1] = 0x23;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xC0;
    WBuffer[1] = 0x07;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xC1;
    WBuffer[1] = 0x78;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xC2;
    WBuffer[1] = 0x78;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0xD0;
    WBuffer[1] = 0x88;
    rgb_reg_writer(WBuffer, 2);

    system_delay_us(100 * 1000);

    WBuffer[0] = 0xE0;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    rgb_reg_writer(WBuffer, 5);

    WBuffer[0] = 0xE1;
    WBuffer[1] = 0x04;
    WBuffer[2] = 0xA0;
    WBuffer[3] = 0x06;
    WBuffer[4] = 0xA0;
    WBuffer[5] = 0x05;
    WBuffer[6] = 0xA0;
    WBuffer[7] = 0x07;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0x00;
    WBuffer[10] = 0x44;
    WBuffer[11] = 0x44;
    rgb_reg_writer(WBuffer, 12);

    WBuffer[0] = 0xE2;
    WBuffer[1] = 0x11;
    WBuffer[2] = 0x11;
    WBuffer[3] = 0x44;
    WBuffer[4] = 0x44;
    WBuffer[5] = 0xE9;
    WBuffer[6] = 0xA0;
    WBuffer[7] = 0xEB;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0xEA;
    WBuffer[10] = 0xA0;
    WBuffer[11] = 0xEC;
    WBuffer[12] = 0xA0;
    WBuffer[13] = 0x00;
    rgb_reg_writer(WBuffer, 14);

    WBuffer[0] = 0xE3;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x11;
    WBuffer[4] = 0x11;
    rgb_reg_writer(WBuffer, 5);

    WBuffer[0] = 0xE4;
    WBuffer[1] = 0x44;
    WBuffer[2] = 0x44;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xE5;
    WBuffer[1] = 0x06;
    WBuffer[2] = 0xEA;
    WBuffer[3] = 0xA0;
    WBuffer[4] = 0xA0;
    WBuffer[5] = 0x08;
    WBuffer[6] = 0xEC;
    WBuffer[7] = 0xA0;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0x0A;
    WBuffer[10] = 0xEE;
    WBuffer[11] = 0xA0;
    WBuffer[12] = 0xA0;
    WBuffer[13] = 0x0C;
    WBuffer[14] = 0xF0;
    WBuffer[15] = 0xA0;
    WBuffer[16] = 0xA0;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xE6;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x11;
    WBuffer[4] = 0x11;
    rgb_reg_writer(WBuffer, 5);

    WBuffer[0] = 0xE7;
    WBuffer[1] = 0x44;
    WBuffer[2] = 0x44;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xE8;
    WBuffer[1] = 0x07;
    WBuffer[2] = 0xEB;
    WBuffer[3] = 0xA0;
    WBuffer[4] = 0xA0;
    WBuffer[5] = 0x09;
    WBuffer[6] = 0xED;
    WBuffer[7] = 0xA0;
    WBuffer[8] = 0xA0;
    WBuffer[9] = 0x0B;
    WBuffer[10] = 0xEF;
    WBuffer[11] = 0xA0;
    WBuffer[12] = 0xA0;
    WBuffer[13] = 0x0D;
    WBuffer[14] = 0xF1;
    WBuffer[15] = 0xA0;
    WBuffer[16] = 0xA0;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xE9;
    WBuffer[1] = 0x36;
    WBuffer[2] = 0x00;
    rgb_reg_writer(WBuffer, 3);

    WBuffer[0] = 0xEB;
    WBuffer[1] = 0x00;
    WBuffer[2] = 0x00;
    WBuffer[3] = 0x4E;
    WBuffer[4] = 0x4E;
    WBuffer[5] = 0xEE;
    WBuffer[6] = 0x44;
    WBuffer[7] = 0x40;
    rgb_reg_writer(WBuffer, 8);

    WBuffer[0] = 0xED;
    WBuffer[1] = 0xFF;
    WBuffer[2] = 0xFF;
    WBuffer[3] = 0x76;
    WBuffer[4] = 0x54;
    WBuffer[5] = 0xC1;
    WBuffer[6] = 0x0F;
    WBuffer[7] = 0xB2;
    WBuffer[8] = 0x3F;
    WBuffer[9] = 0x32;
    WBuffer[10] = 0xBF;
    WBuffer[11] = 0x01;
    WBuffer[12] = 0xC4;
    WBuffer[13] = 0x56;
    WBuffer[14] = 0x7F;
    WBuffer[15] = 0xFF;
    WBuffer[16] = 0xFF;
    rgb_reg_writer(WBuffer, 17);

    WBuffer[0] = 0xFF;
    WBuffer[1] = 0x77;
    WBuffer[2] = 0x01;
    WBuffer[3] = 0x00;
    WBuffer[4] = 0x00;
    WBuffer[5] = 0x00;
    rgb_reg_writer(WBuffer, 6);

    WBuffer[0] = 0x35;
    WBuffer[1] = 0x00;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0x36;
    WBuffer[1] = 0x00;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0x3A;
    WBuffer[1] = 0x66;
    rgb_reg_writer(WBuffer, 2);

    WBuffer[0] = 0x11;  
    rgb_reg_writer(WBuffer,1);

    system_delay_us(200 * 1000);   
    
    WBuffer[0] = 0x29;  
    rgb_reg_writer(WBuffer,1);

    system_delay_us(50 * 1000);   
	#endif
	#endif
    system_delay_us(50 * 1000);  
	
}

void rgb_display_start(struct_Timer_t *TIMERx, struct_RGB_TypeDef_t *hrgb, uint32_t fps, unsigned char *Imagedata)
{
    __TIMER_INIT(TIMERx, fps);
    __TIMER_INT_ENABLE(TIMERx);

    hrgb->rgb_TxData = Imagedata;
    hrgb->VerticalSignalCount = 0;
    hrgb->vs_back_count = 0;
    hrgb->vs_tvw_count = 0;

    __TIMER_START(TIMERx);
}

volatile uint32_t time_cnt1=0;
volatile uint32_t time_cnt2=0;
extern uint32_t get_system_dwt_value();

const uint16_t rgb_color_table[] =
{
    0xf800,
    0x07e0,
    0x001f,
    0xffe0,
    0x07ff,
    0xf81f,
    0xf00f,
    0xffff,
    0x0000,
};

volatile uint8_t te_sign;
uint16_t test_couter = 0;
uint16_t test_id = 0;
__RAM_CODE void rgb_timer_IRQHandler(struct_Timer_t *TIMERx, struct_RGB_TypeDef_t *hrgb)
{
    __TIMER_CLEAR_IQR(TIMERx);
	
    /* If the previous frame is not completed ,return */
    if(hrgb->VerticalSignalCount != 0)
    {
         return;
    }
    //printf("t");
	 te_sign = 0;
	///////////////////printf("t:%d\r\n",(get_system_dwt_value()-time_cnt1)/192);
    /* Vertical signal Start*/
    // __RGB_LCD_VSYNC__RELEASE(); 
    // Line_porch_set(RGB_VSPW);//VSPW
    // __RGB_LCD_VSYNC_SET();
    // Line_porch_set(RGB_VBPD);//VBPD
    test_couter ++;
    if(test_couter >= 66)
    {
        test_couter = 0;
        test_id ++;
        test_id %= 9; 
        for(uint16_t i = 0;i<800;i++)
        {
            test_buff[i] = rgb_color_table[test_id];
        }
    }

    //fputc('>',NULL);


    __RGB_LCD_DENABLE_SET(); 
    __RGB_LCD_VSYNC__RELEASE(); 

    /* set Vertical back porch signal */
    Vertical_back_porch_set();

    /* send First Row */
    __RGB_LCD_DENABLE_RELEASE(); 
    __8080_DATA_WR_LEN(RGB_ROW);
     
    __DMA_TO_8080_START_IT(&hrgb->rgb_TxData[0], RGB_ROW / 2);    
	 time_cnt1 = get_system_dwt_value();
	
}

static uint32_t VerticalSignal_offset = 0;
__RAM_CODE void rgb_dma_IRQHandler(struct_RGB_TypeDef_t *hrgb)
{
	//
    if (__DMA_GET_TFR_STATUS())
    {
        __DMA_CLEAR_TFR_STATUS();

        if(hrgb->vs_back_count < RGB_VBPD)
        {
            hrgb->vs_back_count++;
            hrgb->vs_tvw_count ++;
        }
        else
        {
            hrgb->VerticalSignalCount++;
        }  

    }
    while(!(__8080_TXFIFO_EMPTY()));
    /* Row *Column End of one frame transmission */
    if(hrgb->VerticalSignalCount == (RGB_COLUMN + RGB_VFPD)) //t:10388us
    {
	//	printf("t:%d\r\n",(get_system_dwt_value()-time_cnt1)/192);
        //te_sign = 0;
        hrgb->VerticalSignalCount = 0;
        hrgb->vs_back_count = 0;
        hrgb->vs_tvw_count = 0;
        __RGB_LCD_VSYNC_SET();                
        //Line_porch_set(RGB_VFPD);  //VFP
        return;
    }

    if(hrgb->vs_tvw_count == RGB_VSPW)
    {
        __RGB_LCD_VSYNC_SET();
    }

//    else if(hrgb->VerticalSignalCount == 240)
//	//else if(hrgb->VerticalSignalCount == 400)
//    {
//        te_sign = 1;
//    }

    /* set Vertical back porch signal and back porch signal */
    Vertical_front_porch_set();
    Vertical_back_porch_set();
	
    /* send next Row */
    __RGB_LCD_DENABLE_RELEASE(); 
    __8080_DATA_WR_LEN(RGB_ROW);
    if((hrgb->vs_back_count < RGB_VBPD) || (hrgb->VerticalSignalCount >= RGB_COLUMN))
    {   
        __DMA_TO_8080_START_IT(&hrgb->rgb_TxData[0], RGB_ROW / 2);
    }
    else
    {
        // VerticalSignal_offset = hrgb->VerticalSignalCount % (DEFAULT_DISP_ROW_COL/RGB_ROW);
        // __DMA_TO_8080_START_IT(&hrgb->rgb_TxData[RGB_ROW * VerticalSignal_offset * 2], RGB_ROW / 2);   
        __DMA_TO_8080_START_IT(&hrgb->rgb_TxData[0], RGB_ROW / 2);
    }  
}
 /************************************************************************************
 * @fn      rgb_display_controller_init
 *
 * @brief   rgb_display_controller_init
 *

 */ 
static void rgb_display_controller_init(void)
{
    /* init parallel CLOCK */  
    __SYSTEM_PARALLEL_CLK_ENABLE();
    __SYSTEM_PARALLEL_CLK_SELECT_SPLL();
    __SYSTEM_GPIOA_CLK_ENABLE();
    __SYSTEM_GPIOB_CLK_ENABLE();
    __SYSTEM_GPIOC_CLK_ENABLE();
    __SYSTEM_GPIOD_CLK_ENABLE();   
    __SYSTEM_DMA0_CLK_ENABLE();
    __SYSTEM_DMA1_CLK_ENABLE();
    __SYSTEM_TIMER0_CLK_ENABLE();
   // __SYSTEM_SPI_MASTER1_X8_CLK_ENABLE();
    __SYSTEM_SPI_MASTER0_X8_CLK_ENABLE();
    printf("parallel clock:%d\r\n", system_get_peripheral_clock( PER_CLK_PARALLEL));

    /* RGB io init */ 
    /*
        D0~D15 PC0~15
        DCLK   PB13
    */
    GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = GPIO_PIN_0|
                            GPIO_PIN_1|
                            GPIO_PIN_2|
                            GPIO_PIN_3|
                            // GPIO_PIN_4|
                            // GPIO_PIN_5|
                            GPIO_PIN_6|
                            GPIO_PIN_7|
                            GPIO_PIN_8|
                            GPIO_PIN_9|
                            GPIO_PIN_10|
                            GPIO_PIN_11;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(GPIOB, &GPIO_Handle);  
    
    GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = GPIO_PIN_12|
                            GPIO_PIN_13|
                            GPIO_PIN_14|
                            GPIO_PIN_15;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(GPIOA, &GPIO_Handle);  
    
    
    GPIO_Handle.Alternate = GPIO_FUNCTION_8;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = RGB565_LCD_DCLK_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_DCLK_PORT, &GPIO_Handle);

	GPIO_Handle.Alternate = GPIO_FUNCTION_0;
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_VSYNC_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_VSYNC_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_HSYNC_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_HSYNC_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_DE_EN_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_DE_EN_PORT, &GPIO_Handle);
	
	GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_RESET_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_RESET_PORT, &GPIO_Handle);
	
//    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
//    GPIO_Handle.Pin       = GPIO_PIN_13;
//    GPIO_Handle.Pull      = GPIO_PULLUP;
//    gpio_init(GPIOD, &GPIO_Handle);
    rgb_lcd_reset_release();

    /* backlight */ 
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_BACKLIGHT_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_BACKLIGHT_PORT, &GPIO_Handle); 
	rgb_lcd_backlight_set();
    rgb_lcd_disp_set();//Display control / standby mode selection. Internal pull low.DISP = “Low” : Standby.DISP = “High” : Normal display.
    system_delay_us(1000 * 20);

	#ifdef RGB56_LCD_INIT_CONFIG
    /* SPI CS */ 
    GPIO_Handle.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_Handle.Pin       = RGB565_LCD_SPI_CS_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_SPI_CS_PORT, &GPIO_Handle);
	
	    /* SPI io init */
    // B0,B2 B3
    GPIO_Handle.Alternate = GPIO_FUNCTION_7;
    GPIO_Handle.Mode      = GPIO_MODE_AF_PP;
    GPIO_Handle.Pin       = RGB565_LCD_SPI_CLK_GPIO|RGB565_LCD_SPI_MOSI_GPIO|RGB565_LCD_SPI_MISO_GPIO;
    GPIO_Handle.Pull      = GPIO_PULLUP;
    gpio_init(RGB565_LCD_SPI_CLK_PORT, &GPIO_Handle);  
	
	 /* SPI init */
    spi_handle.SPIx                       = SPIMX8_0;
    spi_handle.Init.Work_Mode             = SPI_WORK_MODE_3;
    spi_handle.Init.Frame_Size            = SPI_FRAME_SIZE_9BIT;
    spi_handle.Init.BaudRate_Prescaler    = 100;
    spi_handle.Init.TxFIFOEmpty_Threshold = 0;
    spi_handle.Init.RxFIFOFull_Threshold  = 0;
    spi_master_init(&spi_handle);
	#endif
    /* PARALLEL Init */
    hparallel.PARALLELx = PARALLEL0;
    hparallel.Init.DataBusSelect = DATA_BUS_16_BIT;
    hparallel.Init.ParallelMode  = MODE_6800; //MODE_8080;//MODE_6800 时钟极性从高到低 DCLK用PB14  MODE_8080时钟极性从低到高 DCLK用PB13
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_0 = 0;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_1 = 1;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_2 = 2;
    hparallel.PARALLELx->DATA_CFG.DATA_TRANS_SEQ_3 = 3;
    hparallel.Init.ReadClock  = WDCLK_DIV_4;
    hparallel.Init.WriteClock = WDCLK_DIV_1;
    parallel_init(&hparallel);
    hparallel.PARALLELx->CRM.WR_L_LEN = 2;
    hparallel.PARALLELx->CRM.WR_H_LEN = 2;   
    __PARALLEL_CS_SET(hparallel.PARALLELx);

    /* DMA Init */
    system_dmac_request_id_config(PARALLEL_INTERFACE,DMA0_REQUEST_ID_3);    
    dma_handle.DMAx                       = DMA0;
    dma_handle.Channel                    = DMA_Channel2;
    dma_handle.Init.Data_Flow             = DMA_M2P_DMAC;
    dma_handle.Init.Request_ID            = DMA0_REQUEST_ID_3;
    dma_handle.Init.Source_Master_Sel     = DMA_AHB_MASTER_3;
    dma_handle.Init.Desination_Master_Sel = DMA_AHB_MASTER_1;
    dma_handle.Init.Source_Inc            = DMA_ADDR_INC_INC;
    dma_handle.Init.Desination_Inc        = DMA_ADDR_INC_NO_CHANGE;
    dma_handle.Init.Source_Width          = DMA_TRANSFER_WIDTH_32;
    dma_handle.Init.Desination_Width      = DMA_TRANSFER_WIDTH_32;
    dma_handle.Init.Source_Burst_Len      = DMA_BURST_LEN_16;
    dma_handle.Init.Desination_Burst_Len  = DMA_BURST_LEN_16;
    dma_init(&dma_handle);

}

 /************************************************************************************
 * @fn      rgb_display_init
 *
 * @brief   rgb_display_init
 *

 */
void rgb_display_init(void* buffer)
{
    coply = test_buff;
    // coply = buffer;

    /* rgb init */
    rgb_init();

    NVIC_ClearPendingIRQ(TIMER0_IRQn);
    NVIC_EnableIRQ(TIMER0_IRQn);
    NVIC_ClearPendingIRQ(DMA0_IRQn);
    NVIC_EnableIRQ(DMA0_IRQn);

    rgb_display_start(Timer0, &rgb_handle, 16, coply);

    printf("%s:%d\r\n", __func__, __LINE__);
}

void stop_refresh_timer(void)
{
    timer_int_disable(Timer0);
    timer_stop(Timer0);
}

void start_refresh_timer(void)
{
    __TIMER_INT_ENABLE(Timer0);
    __TIMER_START(Timer0);
//	te_sign = 0;
//	///////////////////printf("t:%d\r\n",(get_system_dwt_value()-time_cnt1)/192);
//    /* Vertical signal Start*/
//    __RGB_LCD_VSYNC__RELEASE(); 
//    Line_porch_set(4);
//    __RGB_LCD_VSYNC_SET();
//    Line_porch_set(8);
//    /* set Vertical back porch signal */
//    Vertical_back_porch_set();

//    /* send First Row */
//    __RGB_LCD_DENABLE_RELEASE(); 
//    __8080_DATA_WR_LEN(RGB_ROW);
//    __DMA_TO_8080_START_IT(rgb_handle.rgb_TxData[0], RGB_ROW / 2);    
}

void st2568_init(void* buffer)
{
    
    //memset(test_buff,0xaa,sizeof(test_buff));
    for(uint16_t i = 0;i<800;i++)
    {
        test_buff[i] = 0xf800;
    }
    
	rgb_display_controller_init();
	rgb_display_init(buffer);
    

}

void timer0_irq(void)
{
    rgb_timer_IRQHandler(Timer0, &rgb_handle);
}

__RAM_CODE void st2568_rgb_display_dma_irq(void)
{
    if (dma_get_tfr_Status(&dma_handle))
    {
        rgb_dma_IRQHandler(&rgb_handle);
        dma_clear_tfr_Status(&dma_handle);
		 
    }
}
#endif

#endif
